package ie.ul.breakthrough;

import ie.ul.breakthrough.Breakthrough;
import ie.ul.breakthrough.BreakthroughMove;
import ie.ul.breakthrough.Player;

import java.util.ArrayList;
import java.util.Random;

/**
 * This extends the abstract player class defined earlier. It is a
 * simple implementation that randomly picks a move from the set
 * of possible moves.
 *
 */
public class RandomPlayer extends Player {

	/**
	 * Blank class constructor
	 *
	 */
	public RandomPlayer() {}

	/**
	 * Constructor that uses the name from the superclass
	 * @param pName A String with the name of the player
	 */
	public RandomPlayer(String pName) {super(pName);}

	/**
	 * Extended initialize function. Takes a side
	 * for the player
	 * @param pColour A char that sets the side the player is on. 'b' for black,
	 * and 'w' for white
	 */
	public void initialize(char pColour) {
		colour = pColour;
	}

	/**
	 * Makes a move. In this implementation, this just picks
	 * a random move and makes it.
	 */
	public BreakthroughMove makeMove(Breakthrough game) {
		// Initialize the array. The arrays are all templates (generics)
		// so for good practice, they are set to their type inside
		// the angled brackets. In this case the type is BreakthroughMove.
		ArrayList<BreakthroughMove> possibleMoves = new ArrayList<BreakthroughMove>();
		// Get the list of possible moves
		possibleMoves = game.generateMoves(colour);

		// if the list is empty

		int iRandom;
		Random randomizer = new Random();

		iRandom = Math.abs(randomizer.nextInt());
		iRandom = iRandom % possibleMoves.size();

		// return the random move
		return possibleMoves.get(iRandom);

	}
}